/**
 * @FileName: ThemesService.java
 * @Author
 * @Description:
 * @Date 2015年12月28日 下午3:26:32
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.service;

import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import cn.hpclub.server.model.SplashImage;
import cn.hpclub.server.util.ResultCode;

public class SplashImageService extends Service{
    static final int                 TYPE_KAIHU = 1;
    public static SplashImageService service    = new SplashImageService();

    /**
     * 获取查询语句
     */
    public String getSqlExceptSelect(ConcurrentHashMap<String, String> searchInfo){
        String sqlExceptSelect = "FROM splash_image WHERE 1=1";
        for(Entry<String, String> item : searchInfo.entrySet()){
            sqlExceptSelect += buildQuery(item);
        }
        sqlExceptSelect += " ORDER BY enabled DESC, id DESC";

        return sqlExceptSelect;
    }

    private String buildQuery(Entry<String, String> item){
        return " AND " + item.getKey() + " LIKE '%" + item.getValue() + "%'";
    }

    /**
     * @Title: delete
     * @Description:批量删除主题
     * @param ids主题ID
     * @return ResultCode
     */
    public ResultCode delete(String[] ids){
        return deleteRecords("splash_image", ids);
    }

    /**
     * @Title: deleteRecords
     * @Description:批量删除数据
     * @param tableName
     * @param ids
     * @return ResultCode
     */
    private ResultCode deleteRecords(String tableName, String[] ids){
        StringBuffer sb = new StringBuffer();
        sb.append("DELETE FROM " + tableName + " WHERE id IN(");
        if(ids.length > 0){
            for(int i = 0; i < ids.length; i++){
                if(i == ids.length - 1){
                    sb.append("?");
                } else{
                    sb.append("?,");
                }
            }
            sb.append(")");
        } else{
            return ResultCode.PARAM_ERROR;
        }
        boolean result = SplashImage.dao.update(sb.toString(), ids);
        if(result){
            return ResultCode.SUCCESS;
        } else{
            return ResultCode.FAILED;
        }
    }
}
